var confirm = false;
$(function () {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    } else {
        var addressid = $("#addressid").val();
        var addressname = $("#addressname").val();
        var addressphone = $("#addressphone").val();
        var addressdesc = $("#addressdesc").val();
        if (!isempty(addressid)) {
            $("#username").html(addressname);
            $("#phone").html(addressphone);
            $("#address").html(addressdesc);
            $("#no_address").hide();
        } else {
            var jsonstr = "{token:" + token + "}";
            var url = "/except/systemcontroller/getmyaddress.action";
            $.ajax({
                url: url,
                type: 'post',
                dataType: "json",
                data: {
                    page: 0,
                    json: jsonstr,
                    count: 1
                },
                async: false,
                success: function (data) {
                    var code = data.code;
                    var message = data.message;
                    var type = data.type;
                    if (type == "0") {
                        $("#delivery_method").hide();
                        $("#delivery_desc").hide();
                    }
                    if (code == 10001 || code == 10002)
                        location.href = "/except/login.jsp";
                    else if (code == 20000) {
                        var datas = data.data[0];
                        $("#username").html(datas.name);
                        $("#phone").html(datas.phone);
                        $("#address").html(datas.address);
                        $("#addressid").val(datas.address_id);
                        $("#no_address").hide();
                        $("#isnoaddress").val(0);
                        remove_advertising();
                    } else if (code == 20013) {
                        $("#shop_address").hide();
                        $("#no_address").show();
                        $("#isnoaddress").val(1);
                    } else {
                        $("#hint_message").html(message);
                        $("#hintbg").show();
                    }
                },
                error: function () {
                    $("#hint_message").html("网络异常");
                    $("#hintbg").show();
                }
            });
        }
        get_freight();
    }

    $("#delivery_method :radio").each(function () {
        var freight = $("#freight").html();
        var totalmoney = $("#totalmoney").html();
        $(this).click(function () {
            var value = $(this).val();
            var isnoaddress = $("#isnoaddress").val();
            if (value == "0") {
                if (isnoaddress == 1) {
                    $("#delivery_desc").hide();
                    $("#no_address").show();
                } else {
                    $("#shop_address").show();
                    $("#delivery_desc").hide();
                }
                $("#freighinfo").show();
                $("#totalmoney").html(totalmoney);
                $("#paymoney").html(totalmoney);
                $("#money").html(totalmoney);
            } else {
                $("#delivery_desc").show();
                $("#shop_address").hide();
                $("#no_address").hide();
                $("#freighinfo").hide();
                $("#totalmoney").html(totalmoney * 1 - freight * 1);
                $("#paymoney").html(totalmoney * 1 - freight * 1);
                $("#money").html(totalmoney * 1 - freight * 1);
            }
        });
    });

    $("#closeimg").click(function () {
        location.href = "/except/files/home_page.jsp";
    });

    $("#confirm_btn").click(function () {
        if (confirm) {
            var delivery = $("#delivery_method :radio:checked").val();
            location.href = "/except/paysuccess.jsp?indenttype=" + delivery;
        }
    });
});

function add_address() {
    $("#order_frm").attr("action", "/except/files/user/add_new_address.jsp").submit();
}

function get_freight() {
    var addressid = $("#addressid").val();
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    var url = "/except/indentcontroller/preindent.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            addressid: addressid,
            json: jsonstr,
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                $("#freight").html(data.data.freight);
                $("#totalmoney").html(data.data.totalmoney);
                $("#paymoney").html(data.data.totalmoney);
                $("#sparemoney").html(data.data.sparemoney);
                $("#money").html(data.data.totalmoney);
                if (data.data.isenough) {
                    $("#spare_pay").attr("checked", true);
                    $("#wechat_pay").attr("checked", false);
                } else {
                    $("#spare_pay").attr("disabled", true)
                    $("#spare_pay").attr("checked", false);
                    $("#wechat_pay").attr("checked", true);
                }
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
        },
        error: function () {
            $("#hint_message").html("网络异常");
            $("#hintbg").show();
        }
    });
}

function showPopup() {
    if ($(".cqchoosenum").hasClass("pop_payss")) {
        $(".cqchoosenum").removeClass("pop_payss");
        $("#background").hide();
    } else {
        $(".cqchoosenum").addClass("pop_payss");
        $("#background").show();
    }
}

function indent() {
    var addressid = $("#addressid").val();
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    var delivery = $("#delivery_method :radio:checked").val();
    var url = "/except/indentcontroller/indent.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            addressid: addressid,
            json: jsonstr,
            indnettype: delivery,
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                $("#indentnum").val(data.data.indentnum);
                $("#money").val(data.data.totalmoney);
                $(".cqchoosenum").addClass("pop_payss");
                $("#background").show();
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
        }
    });
}

function pay_indent() {
    var requrl = location.href.split('#')[0];
    $(".cqchoosenum").removeClass("pop_payss");
    $("#background").hide();
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var paytype = 1;
    $("#paytype :radio").each(function () {
        var checked = $(this).attr("checked");
        if (checked)
            paytype = $(this).val();
    });
    var indentnum = $("#indentnum").val();
    var jsonstr = "{token:" + token + "}";
    var url = "/except/indentcontroller/payindent.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            indentnum: indentnum,
            paytype: paytype,
            requrl: requrl
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                // alert(data);
                if(paytype == 1) {
                    location.href = "/except/paysuccess.jsp?indenttype=" + delivery;
                } else {
                    var delivery = $("#delivery_method :radio:checked").val();
                    $("#indenttype", delivery);
                    $("#nonce_str").val(data.data.nonce_str);
                    $("#result_sign").val(data.data.result_sign);
                    $("#timestamp").val(data.data.indenttime);
                    $("#prepay_id").val(data.data.prepay_id);
                    $("#payinfo").submit();
                }


                // $("#indenttype").val(delivery);
                // wechatpay(data.data.indenttime, data.data.nonce_str, data.data.signature, data.data.prepay_id, data.data.result_sign, delivery);
                // location.href = "/except/files/pay.jsp?indenttype=" + delivery + "&data=" + data.data;
                // confirm = true;
                // $("#hint_message").html("支付成功");
                // $("#hintbg").show();
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
        }
    });
}

function wechatpay(timestamp, nonceStr, signature, package, paySign, delivery) {

}